<!--
  @Description:自定义导航-目录配置编辑
  @Author:zqy
  @Date:2023-08-29 13:11:11
  @Last Modified by:zqy
  @Last Modified time:2023-08-29 13:11:11
-->
<template>
  <el-dialog
    ref="refDlg"
    :title="`${!!(detailData && detailData.id) ? '编辑' : '新增'}目录基本信息`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :hide-func-bar="true"
    :before-close="clearForm"
    width="500px"
    custom-class="custom-dlg"
    @close="closeDialog"
  >
    <div>
      <el-form ref="form" :model="formData" :rules="formRules" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="目录名称" prop="tabName">
              <el-input v-model="formData.tabName" size="mini" placeholder="请输入目录名称" maxlength="30" show-word-limit />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="是否显示" prop="state">
              <el-switch v-model="formData.state" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col v-if="Number(formData.state || 0) ===1 && isLevelOne" :span="16">
            <el-form-item
              label="显示位置"
              prop="showPlatform"
              :rules="[
                {required:Number(formData.state || 0) ===1 && isLevelOne,message:'请选择显示位置',trigger:'change'}
              ]"
            >
              <el-select v-model="formData.showPlatform" multiple clearable style="width:100%">
                <el-option value="mobile" label="移动端" />
                <el-option value="pad" label="PAD" />
                <el-option value="pc" label="PC" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col v-if="isLastNode" :span="24">
            <el-form-item
              label="图片显示方式"
              prop="picShowType"
              :rules="[
                {required:isLastNode,message:'请选择显示位置',trigger:'change'}
              ]"
            >
              <el-select v-model="formData.picShowType" clearable style="width:100%">
                <el-option value="big" label="大图" />
                <el-option value="small" label="小图" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="目录图片" prop="picUrl">
              <div class="nav-icon-row">
                <qup-img-uploader
                  mask
                  :image-url="cateImage.absoluteUrl"
                  :relative-url="cateImage.relativeUrl"
                  @upload-success="handleUploadSuccess"
                  @remove="handleDelImg"
                />
                <div class="icon-msg">
                  <div>建议图片不超过1M,支持png\jpg\jpeg</div>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import QupImgUploader from '@/components/imgUploader'
import { saveTabConfig } from '@/api/tabConf'
export default {
  name: 'DirectoryConfigEdit',
  components: { QupImgUploader },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    tabDefineId: {
      // 自定义导航ID
      type: String,
      default: ''
    },
    tabShowLevel: {
      // 自定义导航层级
      type: [String, Number],
      default: ''
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        tabName: '',
        parent: '0',
        levelType: 'FIRST', // 层级类型 一级:FIRST 二级:SECOND 三级:THIRD
        state: 1,
        showPlatform: ['mobile'], // 显示平台： mobile 移动端， pad  PAD端 ， pc PC端
        picUrl: '',
        picName: '',
        gotoDTO: {
          gotoType: 'HELP_CENTER_DETAIL_LIST', // 固定参数：目录关联的为帮助中心文章
          categoryDTOList: [],
          filterId: '',
          gotoValue: '',
          gotoName: ''
        },
        productAdvertisementDTOList: [],
        picShowType: '' // 图片显示方式:big 大图  ，  small  小图
      },
      formRules: {
        tabName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请选择是否显示', trigger: 'blur' }
        ]
      },
      cateImage: {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    },
    isLastNode() {
      // 是否末级:自定义导航层级为1级的，不允许新增子级
      if (['1', 1].includes(this.tabShowLevel)) {
        return true
      }
      return ![null, undefined, '', '0', 0].includes(this.detailData.parent)
    }
  },
  watch: {

  },
  created() {
    if (this.detailData) {
      const { parent, levelType } = this.detailData
      this.formData.parent = parent
      this.formData.levelType = levelType
      if (this.detailData.id) {
        this.initFormData(this.detailData)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  methods: {
    initFormData(data) {
      this.formData = {
        tabName: data.tabName || '', // 目录名称
        parent: data.parent || '0',
        levelType: data.levelType || 'FIRST',
        state: ['1', 1, '0', 0].includes(data.state) ? Number(data.state) : 1,
        showPlatform: data.showPlatform || '',
        picUrl: data.picUrl || '',
        picName: data.picName || '',
        picShowType: data.picShowType || '',
        gotoDTO: {
          gotoType: data.gotoType || '',
          categoryDTOList: data.categoryDTOList || [],
          filterId: data.filterId || '',
          gotoValue: data.gotoValue || '',
          gotoName: data.gotoName || ''
        },
        productAdvertisementDTOList: data.productAdvertisementDTOList || []
      }
      if (data.picUrl) {
        this.cateImage = {
          absoluteUrl: this.formatImageUrl(data.picUrl),
          relativeUrl: data.picUrl
        }
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        tabName: '',
        levelType: 'FIRST',
        state: 1,
        showPlatform: '',
        picUrl: '',
        picName: '',
        gotoDTO: {
          gotoType: '',
          categoryDTOList: [],
          filterId: '',
          gotoValue: '',
          gotoName: ''
        },
        productAdvertisementDTOList: []
      }
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
      done && done()
    },
    handleUploadSuccess(res, file, fileList) {
      this.formData.picUrl = res.storeFileName
      this.formData.picName = res.sourceFileName
      this.cateImage = {
        absoluteUrl: res.url,
        relativeUrl: res.storeFileName
      }
    },
    handleDelImg() {
      this.formData.picUrl = ''
      this.formData.picName = ''
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const { gotoDTO } = this.formData

          const showPlatform = this.formData.showPlatform.join(',')
          const formData = {
            ...this.formData,
            ...gotoDTO
          }
          delete formData.gotoDTO
          formData.showPlatform = showPlatform
          formData.v2TabDefineId = this.tabDefineId

          if (this.detailData && this.detailData.id) {
            formData.id = this.detailData.id
          }

          this.submiting = true
          await saveTabConfig(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message.success(`${formData.id ? '编辑' : '新增'}保存成功`)
              this.$emit('success', !!formData.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.nav-icon-row{
  display: flex;
  align-items: center;
  .icon-msg{
    line-height: 1.4;
    font-size: 12px;
    color: #999;
    margin-left: 10px;
  }
}
.el-col el-col-11{margin-bottom: 20px;}
/deep/ .el-select-dropdown__item.selected .picIcon{
  color:#3DC094;
}
</style>
